package com.mall.controller.portal;

import com.mall.common.Const;
import com.mall.common.ServiceRespones;
import com.mall.pojo.User;
import com.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户模块
 */

@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceRespones<User> login(String username, String password, HttpSession session) {
        ServiceRespones<User> respones = iUserService.login(username, password);
        if (respones.isSuccess()) { // 成功
            session.setAttribute(Const.CURRENT_USER, respones.getData());
        }
        return respones;
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do", method = RequestMethod.GET)
    @ResponseBody
    public ServiceRespones<String> logout(HttpSession session) {
        session.removeAttribute(Const.CURRENT_USER);
        return ServiceRespones.createBySuccess();
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do", method = RequestMethod.GET)
    @ResponseBody
    public ServiceRespones<String> register(User user) {
        return iUserService.register(user);
    }

    /**
     * 校验用户名和邮箱
     * @param str
     * @param type
     * @return
     */
    @RequestMapping(value = "checkValid.do", method = RequestMethod.GET)
    @ResponseBody
    public ServiceRespones<String> register(String str, String type) {
        return iUserService.checkValid(str, type);
    }
}
